import 'dart:convert' show json;

import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/components/appbar_plugins.dart';
import 'package:message_tools/components/index.dart';
import 'package:message_tools/router/index.dart';
import 'package:message_tools/store/index.dart';
import 'package:message_tools/store/view_model/sms_task_model.dart';
import 'package:message_tools/ui/page/sms/start_sms_task_page.dart';

class SmsTaskListPage extends StatefulWidget {
  _SmsTaskListPageState createState() => _SmsTaskListPageState();
}

class _SmsTaskListPageState extends State<SmsTaskListPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    Store.value<SmsTaskModel>(context).loadData();

    return Scaffold(
        appBar: AppBarPlugin.Create(
          "群发任务",
          centerTitle: true,
          leading: Builder(builder: (BuildContext c) {
            return IconButton(
                icon: const Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.pop(context);
                });
          }),
          actions: <Widget>[
            IconButton(
                icon: Image.asset('assets/images/add.png'),
                onPressed: () {
                  Application.setString("edit_sms_task", "");

                  Application.router.navigateTo(
                      context, Routes.RouteAddSmsTaskPage,
                      transition: TransitionType.fadeIn);
                })
          ],
        ),
        body: Container(
            color: Colors.grey[100],
            child: Store.connect<SmsTaskModel>(
                builder: (context, snapshot, child) {
              return _buildListViewItem(snapshot.list);
            })));
  }

  Widget _buildListViewItem(List list) {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: list.length, // item 的个数
      itemBuilder: (BuildContext context, int index) {
        var smsTask = list[index];
        return Container(
          margin: const EdgeInsets.all(5),
          padding: const EdgeInsets.all(10),
          decoration: new BoxDecoration(
            //背景
            color: Colors.white,
            //设置四周边框
            border: createBorder(color: Colors.grey[300]),
          ),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Text(smsTask.title,
                      style: TextStyle(
                          height: 1,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Colors.grey[600])),
                ],
              ),
              Divider(
                height: 10.0,
                indent: 0.0,
                color: Colors.grey,
              ),
              Row(
                children: <Widget>[
                  LimitedBox(
                      maxWidth: MediaQuery.of(context).size.width - 32,
                      child: Text(
                        smsTask.smsTemplateContent,
                        softWrap: true,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 100,
                        textAlign: TextAlign.left,
                        style: new TextStyle(
                            height: 1,
                            fontSize: 16,
                            color: Colors.grey[700],
                            letterSpacing: 1.2),
                      ))
                ],
              ),
              Divider(
                height: 10.0,
                indent: 0.0,
                color: Colors.grey,
              ),
              Row(
                children: <Widget>[
                  MaterialButton(
                      child: Text("发送"),
                      onPressed: () {
                        Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (context) => StartSmsTaskPage(smsTask)),
                        );
                      }),
                  MaterialButton(
                      child: Text("修改"),
                      onPressed: () {
                        Application.setString(
                            "edit_sms_task", json.encode(smsTask));

                        Application.router.navigateTo(
                            context, Routes.RouteAddSmsTaskPage,
                            transition: TransitionType.fadeIn);
                      }),
                  MaterialButton(child: Text("删除"), onPressed: () {})
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}
